package com.example.crazy3d;
import android.os.Handler;
import android.os.HandlerThread;
import android.os.Message;
import com.orhanobut.logger.Logger;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;




public class PrintScavengers {
    public HandlerThread PrintScavengers_HandlerThread;
    public Handler PrintScavengers_Handler;
    public PrintScavengers(MainActivity PrintScavengers_MainActivity) {
        PrintScavengers_HandlerThread = new HandlerThread("PrintScavengers_HandlerThread");
        PrintScavengers_HandlerThread.start();
        PrintScavengers_Handler = new Handler(PrintScavengers_HandlerThread.getLooper()) {
            @Override
            public void handleMessage (Message msg) {
                if (msg.what == StatusUtils.PrintScavengers) {
                    Logger.d("path -> "+msg.obj);
                    new Thread(new ProcessPrintData(PrintScavengers_MainActivity ,PrintScavengers_MainActivity.MainHandler, msg.obj), "ProcessPrintData").start();
                }
            }
        };
    }

    static class ProcessPrintData implements Runnable{
        final Handler ProcessPrintData_Handler;
        public Object FilePath;
        public MainActivity ProcessPrintData_MainActivity;
        FileInputStream File_Gcode;
        BufferedReader data_Gcode;
        public ProcessPrintData(MainActivity PrintScavengers_MainActivity,Handler handler, Object path) {
            ProcessPrintData_Handler = handler;
            FilePath = path;
            ProcessPrintData_MainActivity = PrintScavengers_MainActivity;
        }
        @Override
        public void run() {

            Logger.d("收到的文件路径" + FilePath.toString());
            try {
                File_Gcode = new FileInputStream(FilePath.toString());
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            }
            data_Gcode = new BufferedReader(new InputStreamReader(File_Gcode));
            try {
                String line;
                /* 这个方法 文件不能大于1.99GB */
                int TopSize = File_Gcode.available();
                Logger.d("File_Gcode -> "+ TopSize);
                int LinSize = 0;

                while ((line = data_Gcode.readLine()) != null) {
                    LinSize += line.getBytes(StandardCharsets.UTF_8).length;
                    Message ProcessPrintData_msg = Message.obtain();
                    Message PrintProcess_msg = Message.obtain();
                    /* 通知类型 */
                    ProcessPrintData_msg.what = StatusUtils.PrintingPipeline;
                    PrintProcess_msg.what = StatusUtils.PrintingPipeline;
                    /* 原地址 */
                    ProcessPrintData_msg.arg1 = StatusUtils.InformationDictatorship;
                    PrintProcess_msg.arg1 = StatusUtils.InformationDictatorship;
                    /* 目的地址 */
                    ProcessPrintData_msg.arg2 = StatusUtils.PrintScavengers;
                    PrintProcess_msg.arg2 = StatusUtils.MainActivity;
                    /* 数据 */
                    ProcessPrintData_msg.obj = line;
                    ProcessPrintData_Handler.sendMessage(ProcessPrintData_msg);
                    /* 进度数据 */
                    /* 这里不对 的改一下 */
                    PrintProcess_msg.obj = (LinSize/TopSize);
                    ProcessPrintData_Handler.sendMessage(PrintProcess_msg);
                    Thread.sleep(200);
                }
                data_Gcode.close();
                File_Gcode.close();
                Logger.d("read file end");

            } catch (IOException | InterruptedException e) {
                e.printStackTrace();
            }
        }


    }

}
